use std::sync::{Arc, Mutex};
use rustis::client::Client;
use rustis::commands::ConnectionCommands;
use rustis::resp::cmd;

#[allow(unused)]
pub enum CookieOps{
    AddCookie,
    DelCookie,
    UpdateCookie
}
pub struct CookieQueue{
    ops:CookieOps,
    cookie:(String,String)
}
impl CookieQueue {
    #[allow(unused)]
    pub fn new(ops:CookieOps,cookie:(String,String)) -> CookieQueue {
        CookieQueue {
            ops,
            cookie
        }
    }
    pub async fn add_cookie(&self,client:Client) {
        client.send(cmd("SET").arg(self.cookie.0.clone()).arg(self.cookie.1.clone()),None).await.unwrap();
        client.send(cmd("EXPIRE").arg(self.cookie.0.clone()).arg("3600"),None).await.unwrap();
    }
    pub async fn del_cookie(&self,client:Client) {
        client.send(cmd("DEL").arg(self.cookie.0.clone()),None).await.unwrap();
    }
    pub async fn update_cookie(&self,client:Client) {
        client.send(cmd("EXPIRE").arg(self.cookie.0.clone()).arg("3600"),None).await.unwrap();
    }
    pub async fn match_ops(&self,client: Arc<Mutex<Client>>) {
        let client = client.lock().unwrap().clone();
        client.select(0).await.unwrap();
        match self.ops {
            CookieOps::AddCookie => self.add_cookie(client).await,
            CookieOps::DelCookie => self.del_cookie(client).await,
            CookieOps::UpdateCookie => self.update_cookie(client).await
        }
    }
}